<?php
/**
 * 文章模型类
 * @author caoge
 * @version 2012-5-7
 */
class ArticleModel extends BaseModel{
	public $_link = array(
			'ActicleType'=>array(
					'mapping_type'=>BELONGS_TO,
					'class_name'=>'ArticleType',
					'mapping_name'=>'type_info',
					'foreign_key'=>'type_id'
			),
			'ArticleTag'=>array(
					'mapping_type'=>MANY_TO_MANY,
					'class_name'=>'ArticleTag',
					'mapping_name'=>'tag',
					'foreign_key'=>'article_id',
					'relation_foreign_key'=>'tag_id',
					'relation_table'=>'blog_article_tag_relationship'
			)
	);
	
	/**
	 * 根据年月统计文章
	 * 
	 * @author caoge
	 * @version 2012-7-16
	 */
	public function dateCount(){
		$sql = "select date_format(create_time,'%Y-%m') as create_time,count(1) as count 
				from blog_article where del_flag=0 
				group by date_format(create_time,'%Y-%m') DESC";
		$ret = $this->query($sql);
		$array = array();
		foreach ($ret as $k=>$v){
			$temp = explode('-',$ret[$k]['create_time']);
			array_push($array, array('year'=>$temp[0],'month'=>$temp[1],'count'=>$v['count']));
		}
		return $array;
	}
	
	/**
	 * 文章阅读排行列表
	 * @param int $limit
	 * @author caoge
	 * @version 2012-7-16
	 */
	public function getArticleByViewNum($limit = 15){
		return $this->where('del_flag=0')->order('view_num desc,create_time desc')->limit($limit)->field('article_id,title,view_num')->select();
	}
	
	/**
	 * 文章评论排行列表
	 * @param int $limit
	 * @author caoge
	 * @version 2012-7-17
	 */
	public function getArticleByCommentNum($limit = 15){
		return $this->where('del_flag=0')->order('comment_num desc,create_time desc')->limit($limit)->field('article_id,title,comment_num')->select();
	}
}